﻿using B.S.SmartStorPro.Infrastructure.Interfaces;
using B.S.SmartStorPro.Infrastructure;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;


namespace B.S.SmartStorPro.Infrastructure.Interfaces
{
    /// <summary>
    /// 基础仓储实现类，提供通用的增删改查操作。
    /// </summary>
    /// <typeparam name="T">实体类型。</typeparam>
    public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        private readonly SqlSugarClient _db;

        /// <summary>
        /// 构造函数，初始化数据库上下文。
        /// </summary>
        /// <param name="dbContext">数据库上下文实例。</param>
        public BaseRepository(WmsDbContext dbContext)
        {
            _db = dbContext.Db;
        }

        /// <summary>
        /// 异步批量添加实体列表。
        /// </summary>
        /// <param name="entities">要添加的实体列表。</param>
        /// <returns>返回受影响的行数。</returns>
        public async Task<int> AddRangeAsync(IEnumerable<T> entities)
        {
            return await _db.Insertable(entities.ToList()).ExecuteCommandAsync();
        }

        /// <summary>
        /// 异步添加单个实体。
        /// </summary>
        /// <param name="entity">要添加的实体。</param>
        /// <returns>返回受影响的行数。</returns>
        public async Task<int> AddAsync(T entity)
        {
            return await _db.Insertable(entity).ExecuteCommandAsync();
        }

        /// <summary>
        /// 根据 ID 异步删除实体。
        /// </summary>
        /// <param name="id">要删除的实体的 ID。</param>
        /// <returns>返回受影响的行数。</returns>
        public async Task<int> DeleteAsync(object id)
        {
            return await _db.Deleteable<T>(id).ExecuteCommandAsync();
        }

        /// <summary>
        /// 根据 ID 列表异步批量删除实体。
        /// </summary>
        /// <param name="ids">要删除的实体的 ID 列表。</param>
        /// <returns>返回受影响的行数。</returns>
        public async Task<int> DeleteRangeAsync(IEnumerable<object> ids)
        {
            return await _db.Deleteable<T>().In(ids.ToArray()).ExecuteCommandAsync();
        }

        /// <summary>
        /// 根据条件异步查询实体列表。
        /// </summary>
        /// <param name="predicate">查询条件。</param>
        /// <returns>返回符合条件的实体列表。</returns>
        public async Task<IList<T>> FindAsync(Expression<Func<T, bool>> predicate)
        {
            return await _db.Queryable<T>().Where(predicate).ToListAsync();
        }

        /// <summary>
        /// 异步获取所有实体的列表。
        /// </summary>
        /// <returns>返回所有实体的列表。</returns>
        public async Task<IList<T>> GetAllAsync()
        {
            return await _db.Queryable<T>().ToListAsync();
        }

        /// <summary>
        /// 异步获取实体总数。
        /// </summary>
        /// <returns>返回实体总数。</returns>
        public async Task<int> CountAsync()
        {
            return await _db.Queryable<T>().CountAsync();
        }

        /// <summary>
        /// 根据 ID 异步获取单个实体。
        /// </summary>
        /// <param name="id">要检索的实体的 ID。</param>
        /// <returns>返回找到的实体。</returns>
        public async Task<T> GetByIdAsync(object id)
        {
            return await _db.Queryable<T>().InSingleAsync(id);
        }

        /// <summary>
        /// 异步更新单个实体。
        /// </summary>
        /// <param name="entity">要更新的实体。</param>
        /// <returns>返回受影响的行数。</returns>
        public async Task<int> UpdateAsync(T entity)
        {
            return await _db.Updateable(entity).ExecuteCommandAsync();
        }

        /// <summary>
        /// 异步批量更新实体列表。
        /// </summary>
        /// <param name="entities">要更新的实体列表。</param>
        /// <returns>返回受影响的行数。</returns>
        public async Task<int> UpdateRangeAsync(IEnumerable<T> entities)
        {
            return await _db.Updateable(entities.ToList()).ExecuteCommandAsync();
        }
    }
}

   